<template>
	<view class="all-main">
		<view class="main">
			<view class="notapply">
				<view class="bill-msg">
					<u-icon name="info-circle-fill" color="#fa3534" size="30"></u-icon>
					<text>请先上传身份证(正、反面)，然后仔细阅读承诺函，并且签名确认。</text>
				</view>
				<view class="error-msg">
					<text>审核失败原因：{{bill_apply.check_msg}}</text>
				</view>
				<view class="idcard-front">
					<u-upload :fileList="positive" @afterRead="_positive" @delete="delete_positive" name="1"
						imageMode="aspectFit" :maxCount="1" width="200" height="150">
						<image v-if="bill_apply.idcard_front != ''" :src="api_url + bill_apply.idcard_front" mode="aspectFit"
							style="width: 200px; height: 150px;"></image>
					</u-upload>
				</view>
				<view class="idcard-back">
					<u-upload :fileList="reverse" @afterRead="_reverse" @delete="delete_reverse" name="2"
						imageMode="aspectFit" :maxCount="1" width="200" height="150">
						<image v-if="bill_apply.idcard_back != ''" :src="api_url + bill_apply.idcard_back" mode="aspectFit"
							style="width: 200px; height: 150px;"></image>
					</u-upload>
				</view>
				<view class="agreement">
					<checkbox value="checked" disabled="true" :checked="checked" style="transform:scale(0.7)" />
					<view class="msg" @click="agreement">
						请认真阅读
						<text>《承诺函》</text>
						，并确认签名
					</view>
				</view>
				<view class="btns" @click="applyClick">
					申请
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		pathToBase64,
		base64ToPath
	} from '@/js_sdk/mmmm-image-tools/index.js'
	var util = require('@/util/request.js');
	export default {
		data() {
			return {
				bill_apply: {},
				positive: [],
				positive_file: '',
				reverse: [],
				reverse_file: '',
				nmb: 0,
				checked: false,
				signFile: '',
				userinfo: {},
				api_url: '',
			};
		},
		onLoad() {
			this.userinfo = uni.getStorageSync('userinfo');
			this.api_url = util.API_URL
			this.loadData()
		},
		onShow() {
			let that = this
			uni.$on('signFile', function(data) {
				that.signFile = data
				if (data != null || data != '') {
					that.checked = true
				}
			})
		},
		methods: {
			// 加载入账申请
			async loadData() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'bill/getBillApply'
				});
				uni.hideLoading()
				if (res.code == 200) {
					this.bill_apply = res.data
					this.positive_file = res.data.idcard_front
					this.reverse_file = res.data.idcard_back
				}
			},
			// 图片上传
			async uploadFile(index, base) {
				uni.showLoading({
					title: '上传中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'common/uploadFile',
					data: {
						file: base
					},
				});
				uni.hideLoading()
				if (res.code == 200) {
					if (index == 1) {
						this.positive_file = res.data.url
					} else if (index == 2) {
						this.reverse_file = res.data.url
					}
				}
			},
			// 人像添加
			_positive(e) {
				let _this = this
				pathToBase64(e.file.url)
					.then(base64 => {
						_this.uploadFile(1, base64)
						_this.positive = [{
							url: e.file.url
						}]
						_this.nmb++
					})
					.catch(error => {
						console.error(error)
					})
			},
			// 人像删除
			delete_positive(e) {
				this.positive = []
				this.positive_file = ""
				this.nmb--
			},
			// 国徽上传
			_reverse(e) {
				let _this = this
				pathToBase64(e.file.url)
					.then(base64 => {
						_this.uploadFile(2, base64)
						_this.reverse = [{
							url: e.file.url
						}]
						_this.nmb++
					})
					.catch(error => {
						console.error(error)
					})
			},
			// 国徽删除
			delete_reverse(e) {
				this.reverse = []
				this.reverse_file = ""
				this.nmb--
			},
			// 点击协议
			agreement() {
				uni.navigateTo({
					url: '/pages/mine/bill/agreement'
				})
			},
			// 账单申请提交
			applyClick() {
				let that = this
				if (this.positive_file == '') {
					uni.showToast({
						title: '请上传身份证人像面',
						duration: 2000,
						icon: 'none'
					});
					return false
				}
				if (this.reverse_file == '') {
					uni.showToast({
						title: '请上传身份证国徽面',
						duration: 2000,
						icon: 'none'
					});
					return false
				}
				if (!this.checked) {
					uni.showModal({
						title: '提示',
						content: '请认真阅读《承诺函》并确认签名',
						confirmText: '去阅读并签名',
						cancelText: '取消',
						showConfirmButton: true,
						showCancelButton: true,
						success: function(ress) {
							if (ress.confirm) {
								that.agreement()
							}
						}
					});
					return false
				}
				uni.showModal({
					title: '提示',
					content: '确定要提交申请吗？',
					success: function(ress) {
						if (ress.confirm) {
							that.applyFun()
						}
					}
				});
			},
			// 账单申请提交 - API
			async applyFun() {
				var that = this
				uni.showLoading({
					title: '提交中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'bill/setBillApplyEdit',
					data: {
						idcard_front: this.positive_file,
						idcard_back: this.reverse_file,
						sign_path: this.signFile
					}
				});
				uni.hideLoading()
				uni.showToast({
					title: res.msg,
					duration: 2000,
					icon: 'none'
				});
				if (res.code == 200) {
					setTimeout(function() {
						uni.navigateBack({
							delta: 1
						})
					}, 1000)
				}
			},
		}
	}
</script>

<style lang="scss">
	.all-main {
		.main {
			padding: 30rpx;

			.notapply {
				position: relative;

				.bill-msg {
					display: flex;
					background-color: #ffffff;
					padding: 30rpx;
					border-radius: 10rpx;

					text {
						padding-left: 20rpx;
						font-size: 28rpx;
						color: #fa3534;
					}
				}

				.error-msg {
					margin-top: 30rpx;
					background-color: #ffffff;
					padding: 30rpx;
					border-radius: 10rpx;
					font-size: 24rpx;
					color: #fa3534;
				}

				.idcard-front {
					padding: 20rpx;
					margin-top: 30rpx;
					background-color: #ffffff;
					border-radius: 10rpx;
					display: flex;
					justify-content: center;
				}

				.idcard-back {
					padding: 20rpx;
					margin-top: 30rpx;
					background-color: #ffffff;
					border-radius: 10rpx;
					display: flex;
					justify-content: center;
				}

				.u-upload {
					flex: 0 !important;
				}

				.agreement {
					margin-top: 30rpx;
					display: flex;
					align-items: center;

					.msg {
						font-size: 24rpx;

						text {
							color: #2979ff;
						}
					}
				}

				.btns {
					position: fixed;
					bottom: 0;
					left: 0;
					right: 0;
					padding: 30rpx;
					text-align: center;
					background-color: #2979ff;
					font-size: 34rpx;
					color: #ffffff;
				}
			}
		}
	}
</style>